# !/usr/bin/python3
# -*- coding: utf-8 -*-
import json

import httpx


def load_weather(city_name):
    weather_url = "http://wthrcdn.etouch.cn/weather_mini?city=" + city_name
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
    }
    # print(weather_url)
    weather_content = get_content(weather_url, headers)
    weather_data = json.loads(weather_content)
    # print(weather_data)
    weather_dict = dict()
    weather_dict['city'] = weather_data['data']['city']
    weather_dict['type'] = weather_data['data']['forecast'][0]['type']
    weather_dict['high'] = weather_data['data']['forecast'][0]['high']
    weather_dict['low'] = weather_data['data']['forecast'][0]['low']
    weather_dict['fengxiang'] = weather_data['data']['forecast'][0]['fengxiang']
    weather_dict['ganmao'] = weather_data['data']['ganmao']
    # print(weather_dict)

    logInfo = ""
    for i, value in enumerate(weather_dict.values()):
        logInfo += str(value)
        if i < len(weather_dict.values()) - 1:
            logInfo += ", "
        else:
            logInfo += "\n"
    print(logInfo)
    return logInfo


def get_content(url, headers):
    client = httpx.Client(http2=True, verify=False)
    response = client.get(url=url, headers=headers)
    return response.content


if __name__ == '__main__':
    load_weather("长沙")

